package cn.fintecher.pangolin.service.business.model.request;

import cn.fintecher.pangolin.common.enums.AssistStatus;
import cn.fintecher.pangolin.common.enums.CaseDataStatus;
import cn.fintecher.pangolin.common.enums.CaseType;
import cn.fintecher.pangolin.common.enums.PublicCaseFlag;
import cn.fintecher.pangolin.common.utils.ZWDateUtil;
import cn.fintecher.pangolin.entity.mysql.QAssistCase;
import cn.fintecher.pangolin.entity.mysql.QCaseInfo;
import com.querydsl.core.BooleanBuilder;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.commons.lang.StringUtils;
import org.joda.time.DateTime;

import java.util.Objects;

/**
 * @Auther: xiaqun
 * @Description: app案件查询请求参数
 * @Date: 10:50 2019/11/14
 */

@Data
public class FindAppCaseRequest {
    @ApiModelProperty(value = "姓名")
    private String personalName;

    @ApiModelProperty(value = "地址")
    private String addressDetail;

    @ApiModelProperty(value = "标识 0-待外访 1-外访中")
    private Integer flag;

    @ApiModelProperty(value = "类型 0-即将到期 1-近3天到期")
    private Integer type;

    @ApiModelProperty(value = "用户ID")
    private String userId;

    @ApiModelProperty(value = "部门code")
    private String code;

    public BooleanBuilder getBuilder() {
        BooleanBuilder builder = new BooleanBuilder();
        QAssistCase qAssistCase = QAssistCase.assistCase;
        builder.and(qAssistCase.assistStatus.eq(AssistStatus.ASSIST_COLLECTING));
        if (Objects.nonNull(this.code)) {
            builder.and(qAssistCase.assistCollector.department.departCode.like(this.code + "%"));
        }
        if (Objects.nonNull(this.userId)) {
            builder.and(qAssistCase.assistCollector.id.eq(this.userId));
        }
        if (StringUtils.isNotBlank(this.personalName)) {
            builder.and(qAssistCase.personalName.like("%" + StringUtils.trim(this.personalName) + "%"));
        }
        if (StringUtils.isNotBlank(this.addressDetail)) {
            builder.and(qAssistCase.addressDetail.like("%" + StringUtils.trim(this.addressDetail) + "%"));
        }
        if (Objects.nonNull(this.flag)) {
            if (this.flag.equals(0)) {
                builder.and(qAssistCase.lastContactResult.isNull());
            } else {
                builder.and(qAssistCase.lastContactResult.isNotNull());
            }
        }
        DateTime dateTime = new DateTime(ZWDateUtil.getNowDate());
        if (Objects.nonNull(this.type)) {
            if (this.type.equals(0)) {
                builder.and(qAssistCase.visitTimeEnd.eq(dateTime.toDate()));
            } else if (this.type.equals(1)) {
                builder.and(qAssistCase.visitTimeEnd.goe(dateTime.toDate())).and(qAssistCase.visitTimeEnd.lt(dateTime.plusDays(4).toDate()));
            }
        }
        return builder;
    }

    public BooleanBuilder getCaseBuilder() {
        BooleanBuilder builder = new BooleanBuilder();
        QCaseInfo qCaseInfo = QCaseInfo.caseInfo;
        builder.and(qCaseInfo.caseType.eq(CaseType.VISIT_CASE));
        builder.and(qCaseInfo.caseDataStatus.eq(CaseDataStatus.IN_POOL));
        builder.and(qCaseInfo.publicCaseFlag.eq(PublicCaseFlag.NO_PUBLIC_CASE));
        if (Objects.nonNull(this.code)) {
            builder.and(qCaseInfo.currentCollector.department.departCode.like(this.code + "%"));
        }
        if (Objects.nonNull(this.userId)) {
            builder.and(qCaseInfo.currentCollector.id.eq(this.userId));
        }
        if (StringUtils.isNotBlank(this.personalName)) {
            builder.and(qCaseInfo.personalName.like("%" + StringUtils.trim(this.personalName) + "%"));
        }
        if (StringUtils.isNotBlank(this.addressDetail)) {
            builder.and(qCaseInfo.address.like("%" + StringUtils.trim(this.addressDetail) + "%"));
        }
        if (Objects.nonNull(this.flag)) {
            if (this.flag.equals(0)) {
                builder.and(qCaseInfo.lastContactResult.isNull());
            } else {
                builder.and(qCaseInfo.lastContactResult.isNotNull());
            }
        }
        return builder;
    }
}
